package com.oa.ylms.apps.business.archives.impl;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.oa.ylms.apps.business.archives.IMailInfoService;
import com.oa.ylms.apps.common.MD5;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.archives.IMailInfoDao;
import com.oa.ylms.apps.entity.archives.MailInfoValue;

public class MailInfoServiceImpl implements IMailInfoService {
	private IMailInfoDao infoDao;
	private MailInfoValue mailInfo;

	@Override
	public int addMailInfo(String mail) throws Exception {
		mailInfo.setMail(mail);
		mailInfo.setVerifyCode(getVerifyCode());
		mailInfo.setActivity(IDEDStates.DISABLE);
		return infoDao.addMailInfo(mailInfo);
	}

	@Override
	public boolean modifyMailInfo(int mailId, String mail) throws Exception {
		mailInfo = queryByMailId(mailId);
		if(mailInfo != null){
			mailInfo.setMail(mail);
			mailInfo.setVerifyCode(getVerifyCode());
			mailInfo.setActivity(IDEDStates.DISABLE);
			infoDao.editMailInfo(mailInfo);
		}
		return false;
	}

	@Override
	public MailInfoValue queryByMailId(int mailId) throws Exception {
		mailInfo.setMailId(mailId);
		return infoDao.queryByMailId(mailInfo);
	}

	@Override
	public boolean verifyMailInfo(int mailId, String verifyCode)
			throws Exception {
		mailInfo = queryMailInfo(mailId, verifyCode);
		if(mailInfo != null && mailInfo.getActivity() == IDEDStates.ENABLE){
			toDisable(mailId);
			return true;
		}
		return false;
	}

	@Override
	public boolean toDisable(int mailId) throws Exception {
		mailInfo = queryByMailId(mailId);
		if(mailInfo != null){
			mailInfo.setActivity(IDEDStates.DISABLE);
			infoDao.editMailInfo(mailInfo);
		}
		return false;
	}

	@Override
	public MailInfoValue queryMailInfo(int mailId, String verifyCode)
			throws Exception {
		mailInfo.setMailId(mailId);
		mailInfo.setVerifyCode(verifyCode);
		for(MailInfoValue miv : infoDao.queryMailInfo(mailInfo)){
			return miv;
		}
		return null;
	}
	
	@Override
	public String getVerifyCode() throws Exception {
		// 随机产生一个16位的验证码
		String sourcenum = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String siglenum = "";
		// 该变量保存验证码
		String verifyCode = "";
		for (int i = 0; i < 16; i++) {
			int index = (int) (Math.random() * 100) % 61;
			siglenum = sourcenum.substring(index, index + 1);
			verifyCode += siglenum;
		}
		return verifyCode;
	}
	
//	@Override
//	public boolean sendMail(Integer mailId) throws Exception{
//		 	mailInfo=queryByMailId(mailId);
//		 	MD5 md5 = new MD5();
//		 	String md5_Mail = md5.getMD5ofStr(mailInfo.getMail());
//		 	String md5_VerifyCode = md5.getMD5ofStr(mailInfo.getVerifyCode());
//	        String host = "smtp.qq.com";//邮件服务器 
//	        String from = "403169754@qq.com";//发件人地址 
//	        //String to = mailInfo.getMail();//接受地址（必须支持pop3协议） 
//	        String to = "yinshijing@vip.qq.com";
//	        String userName = "403169754";//发件人邮件名称 
//	        String pwd = "1314520!ysj";//发件人邮件密码 
//	        Properties props = new Properties(); 
//	        props.put("mail.smtp.host", host); 
//	        props.put("mail.smtp.auth", "true"); 
//	         
//	        Session session = Session.getDefaultInstance(props); 
//	        session.setDebug(true); 
//	         
//	        MimeMessage msg = new MimeMessage(session); 
//	        try{
//	        msg.setFrom(new InternetAddress(from)); 
//	        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//发送 
//	        msg.setSubject("计算机工程学院团委办公自动化管理系统--邮箱确认信");//邮件主题 
//	        msg.setText("你好,<br/>为了确认你们的邮箱，并在今后工作中使用到。请点击如下链接： http://localhost:8080/ylms/verfiy.action?mail="+md5_Mail+"&verifyCode="+md5_VerifyCode);//邮件内容 
//	        msg.saveChanges(); 
//	             
//	        Transport transport = session.getTransport("smtp"); 
//	        transport.connect(host, userName, pwd);//邮件服务器验证 
//	        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO)); 
//	        System.out.println("send ok..........................."); 
//	        }catch(Exception e){
//	        	return false;
//	        }
//	        return true;
//	} 
	
	public MailInfoValue getMailInfo() {
		return mailInfo;
	}

	public void setMailInfo(MailInfoValue mailInfo) {
		this.mailInfo = mailInfo;
	}
	
	public IMailInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IMailInfoDao infoDao) {
		this.infoDao = infoDao;
	}

}
